package cn.edu.gcu.giftems.daos;

import cn.edu.gcu.giftems.pojos.OrderItem;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单单项Dao类.
 *
 * @author ZhengHQ
 * @create 2018-09-24
 * @see
 */
public class OrderItemDao extends HibernateDaoSupport {
    
    //获取订单单项总记录
    public int findCount() {
        String hql = "select count(*) from OrderItem";
        //List list=new ArrayList();
        List<Long> list = (List<Long>)this.getHibernateTemplate().find(hql);
        if(list.size() > 0){
            return list.get(0).intValue();
        }
        return 0;
    }
    //获取订单单项集合信息
    public List<OrderItem> findByPage(int begin, int pageSize) {
        try{
            String hql =  "select new OrderItem(oi.id, o.id, g.id, g.name, oi.quantity, oi.purchasePrice)" +
                    "from OrderItem oi, cn.edu.gcu.giftems.pojos.Order o, Gift g " +
                    "where oi.order = o.id and oi.gift = g.id order by oi.id";
            List<OrderItem> list = (List<OrderItem>)this.getHibernateTemplate().execute(
                    new HibernateCallback<Object>() {
                        public Object doInHibernate(Session arg0)
                                throws HibernateException {
                            Query query = arg0.createQuery(hql);
                            query.setFirstResult(begin);
                            query.setMaxResults(pageSize);
                            return query.list();
                        }
                    }
            );
            return list;
        }catch (RuntimeException re) {
            throw re;
        }
    }
    //添加订单单项
    public void save(OrderItem orderItem) {
        this.getHibernateTemplate().save(orderItem);
    }
    
    //根据订单单项编号查询订单单项信息
    public OrderItem finById(int id) {
        return this.getHibernateTemplate().get(OrderItem.class,id);
    }
    
    //更新订单单项信息
    public void update(OrderItem orderItem) {
        this.getHibernateTemplate().update(orderItem);
    }
    
    //根据订单单项id删除订单单项
    public void delete(OrderItem orderItem) {
        this.getHibernateTemplate().delete(orderItem);
    }

    public List<OrderItem[]> rank() {
        List typeId = this.getHibernateTemplate().find("select id from Type");
        List<OrderItem[]> items = new ArrayList<OrderItem[]>();
        for (int i=0; i<typeId.size(); i++) {
            String hql =  "select new OrderItem(t.name, g.name, SUM(oi.quantity)) " +
                    "from OrderItem oi, Gift g, cn.edu.gcu.giftems.pojos.Type t " +
                    "where oi.gift = g.id and g.type = t.id and t.id = " + typeId.get(i) +
                    "group by g.name order by SUM(oi.quantity) desc";
            List<OrderItem> list = (List<OrderItem>)this.getHibernateTemplate().execute(
                    new HibernateCallback<Object>() {
                        public Object doInHibernate(Session arg0)
                                throws HibernateException {
                            Query query = arg0.createQuery(hql);
                            query.setFirstResult(0);
                            query.setMaxResults(10);
                            return query.list();
                        }
                    }
            );
            OrderItem[] oi = new OrderItem[list.size()];
            for (int j=0;j<list.size();j++) {
                oi[j] = list.get(j);
            }
            items.add(i,oi);
        }

        return items;
    }
}
